package com.witdata.security.filter;

import com.witdata.mysql.base.service.UserLearningSessionService;
import com.witdata.mysql.domain.entity.UserLearningSession;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.IOException;

import static com.witdata.common.constant.RedisConstant.REDISKEYLOGIN;


@RequiredArgsConstructor
@WebFilter("/*")
public class Myfilter implements Filter {
    private static final Logger logger = LoggerFactory.getLogger(Myfilter.class);
    private final UserLearningSessionService userLearningSessionService;
    private final RedisTemplate<String, String> redisTemplate;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
//        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
//        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
//        String requestURI = httpServletRequest.getRequestURI();
//
//        logger.debug("Filtering request: {}", requestURI);
//
//        // 排除登录页面，避免干扰
//        if (requestURI.equals("/auth/login") || requestURI.equals("/auth/register") || requestURI.equals("/user/info") || requestURI.equals("/auth/captcha")) {
//            filterChain.doFilter(servletRequest, servletResponse);
//            return;
//        }
//        if(requestURI.equals("/auth/logout")){
//            filterChain.doFilter(servletRequest, servletResponse);
//            redisTemplate.delete(REDISKEYLOGIN+"userName");
//            userLearningSessionService.updateUserActivity(redisTemplate.opsForValue().get(REDISKEYLOGIN+"userName"));
//        }

//        if (redisTemplate.opsForValue().get(REDISKEYLOGIN+"userName")==null) {
//            logger.warn("No session or username found, returning 401");
//            httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED); // 设置 401 状态码
//            httpServletResponse.setContentType("application/json"); // 设置响应类型为 JSON
////            httpServletResponse.getWriter().write("{\"error\": \"Unauthorized, please login\"}"); // 返回错误信息
//            redisTemplate.delete(REDISKEYLOGIN+"userName");
//            return;
//        }

//        String userName = (String) session.getAttribute("username");
//        String userName = redisTemplate.opsForValue().get(REDISKEYLOGIN+"userName");
//        UserLearningSession byUsername = userLearningSessionService.findByUsername(userName);

        // 检查用户状态
//        if (byUsername == null || "INACTIVE".equals(byUsername.getStatus())) {
//            logger.warn("User {} is inactive or not found, returning 401", userName);
//            httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED); // 设置 401 状态码
//            httpServletResponse.setContentType("application/json"); // 设置响应类型为 JSON
//            httpServletResponse.getWriter().write("{\"error\": \"User inactive or not found, please login\"}"); // 返回错误信息
//            redisTemplate.delete(REDISKEYLOGIN+"userName");
//            return;
//        }

        // 用户状态为 ACTIVE，更新活动
//        userLearningSessionService.updateUserActivity(userName);
        filterChain.doFilter(servletRequest, servletResponse);
    }
}